// 日期格式化函数
function formatPickerDate(date) {
  // 获取年月日数组，格式为 ['2021', '01', '23']
  const yearMonthDay = [
    date.getFullYear().toString(),
    (date.getMonth() + 1).toString().padStart(2, "0"),
    date.getDate().toString().padStart(2, "0"),
  ];

  // 获取时分数组，格式为 ['23', '01']
  const hourMinute = [
    date.getHours().toString().padStart(2, "0"),
    date.getMinutes().toString().padStart(2, "0"),
  ];

  return { yearMonthDay, hourMinute };
}

function getXunShou(ganzhi) {
  let jiazi = [
    "甲子",
    "乙丑",
    "丙寅",
    "丁卯",
    "戊辰",
    "己巳",
    "庚午",
    "辛未",
    "壬申",
    "癸酉",
  ];
  let jiaxu = [
    "甲戌",
    "乙亥",
    "丙子",
    "丁丑",
    "戊寅",
    "己卯",
    "庚辰",
    "辛巳",
    "壬午",
    "癸未",
  ];
  let jiashen = [
    "甲申",
    "乙酉",
    "丙戌",
    "丁亥",
    "戊子",
    "己丑",
    "庚寅",
    "辛卯",
    "壬辰",
    "癸巳",
  ];
  let jiawu = [
    "甲午",
    "乙未",
    "丙申",
    "丁酉",
    "戊戌",
    "己亥",
    "庚子",
    "辛丑",
    "壬寅",
    "癸卯",
  ];
  let jiachen = [
    "甲辰",
    "乙巳",
    "丙午",
    "丁未",
    "戊申",
    "己酉",
    "庚戌",
    "辛亥",
    "壬子",
    "癸丑",
  ];
  let jiayin = [
    "甲寅",
    "乙卯",
    "丙辰",
    "丁巳",
    "戊午",
    "己未",
    "庚申",
    "辛酉",
    "壬戌",
    "癸亥",
  ];
  if (jiazi.includes(ganzhi)) {
    return "子";
  } else if (jiaxu.includes(ganzhi)) {
    return "戌";
  } else if (jiashen.includes(ganzhi)) {
    return "申";
  } else if (jiawu.includes(ganzhi)) {
    return "午";
  } else if (jiachen.includes(ganzhi)) {
    return "辰";
  } else if (jiayin.includes(ganzhi)) {
    return "寅";
  }
}
function getXunKong(ganzhi) {
  let jiazi = [
    "甲子",
    "乙丑",
    "丙寅",
    "丁卯",
    "戊辰",
    "己巳",
    "庚午",
    "辛未",
    "壬申",
    "癸酉",
  ];
  let jiaxu = [
    "甲戌",
    "乙亥",
    "丙子",
    "丁丑",
    "戊寅",
    "己卯",
    "庚辰",
    "辛巳",
    "壬午",
    "癸未",
  ];
  let jiashen = [
    "甲申",
    "乙酉",
    "丙戌",
    "丁亥",
    "戊子",
    "己丑",
    "庚寅",
    "辛卯",
    "壬辰",
    "癸巳",
  ];
  let jiawu = [
    "甲午",
    "乙未",
    "丙申",
    "丁酉",
    "戊戌",
    "己亥",
    "庚子",
    "辛丑",
    "壬寅",
    "癸卯",
  ];
  let jiachen = [
    "甲辰",
    "乙巳",
    "丙午",
    "丁未",
    "戊申",
    "己酉",
    "庚戌",
    "辛亥",
    "壬子",
    "癸丑",
  ];
  let jiayin = [
    "甲寅",
    "乙卯",
    "丙辰",
    "丁巳",
    "戊午",
    "己未",
    "庚申",
    "辛酉",
    "壬戌",
    "癸亥",
  ];
  if (jiazi.includes(ganzhi)) {
    return "戌亥";
  } else if (jiaxu.includes(ganzhi)) {
    return "申酉";
  } else if (jiashen.includes(ganzhi)) {
    return "午未";
  } else if (jiawu.includes(ganzhi)) {
    return "辰巳";
  } else if (jiachen.includes(ganzhi)) {
    return "寅卯";
  } else if (jiayin.includes(ganzhi)) {
    return "子丑";
  }
}
function getDizhiIdex(dizhi) {
  const dizhiList = [
    "子",
    "丑",
    "寅",
    "卯",
    "辰",
    "巳",
    "午",
    "未",
    "申",
    "酉",
    "戌",
    "亥",
  ];
  return dizhiList.indexOf(dizhi);
}
function getTianganIndex(tiangan) {
  const tianganList = [
    "甲",
    "乙",
    "丙",
    "丁",
    "戊",
    "己",
    "庚",
    "辛",
    "壬",
    "癸",
  ];
  return tianganList.indexOf(tiangan);
}
// 自定义格式化日期函数
const zidingyiFormatDatetime = (type, val) => {
  if (type === "year") {
    return val + "年";
  }
  if (type === "month") {
    return val + "月";
  }
  if (type === "day") {
    return val + "日";
  }
  if (type === "hour") {
    return val + "时";
  }
  if (type === "minute") {
    return val + "分";
  }
  return val;
};
function adjustHours(date, direction) {
  // 验证输入参数
  if (!(date instanceof Date) || isNaN(date.getTime())) {
    throw new Error("Invalid Date object");
  }

  if (direction !== 1 && direction !== -1) {
    throw new Error("Direction must be 1 (add) or -1 (subtract)");
  }

  // 调整小时数（2 小时 = 2 * 60 * 60 * 1000 毫秒）
  date.setTime(date.getTime() + direction * 2 * 60 * 60 * 1000);

  return date;
}
function getLiuQinByGanZhi(tiangan, dizhi) {
  // 天干五行映射
  const tianganElements = {
    甲: "木",
    乙: "木",
    丙: "火",
    丁: "火",
    戊: "土",
    己: "土",
    庚: "金",
    辛: "金",
    壬: "水",
    癸: "水",
  };

  // 地支五行映射
  const dizhiElements = {
    子: "水",
    丑: "土",
    寅: "木",
    卯: "木",
    辰: "土",
    巳: "火",
    午: "火",
    未: "土",
    申: "金",
    酉: "金",
    戌: "土",
    亥: "水",
  };

  // 六亲关系映射（基于五行生克）
  const sixRelativesMap = {
    生我: "父母",
    我生: "子孙",
    克我: "官鬼",
    我克: "妻财",
    同我: "兄弟",
  };

  // 检查输入有效性
  if (!tianganElements[tiangan]) {
    return `错误：无效的天干 "${tiangan}"`;
  }
  if (!dizhiElements[dizhi]) {
    return `错误：无效的地支 "${dizhi}"`;
  }

  // 获取五行属性
  const myElement = tianganElements[tiangan];
  const targetElement = dizhiElements[dizhi];

  // 判断五行生克关系
  let relation;
  if (myElement === targetElement) {
    relation = "同我";
  } else if (
    (myElement === "木" && targetElement === "土") ||
    (myElement === "火" && targetElement === "金") ||
    (myElement === "土" && targetElement === "水") ||
    (myElement === "金" && targetElement === "木") ||
    (myElement === "水" && targetElement === "火")
  ) {
    relation = "我克";
  } else if (
    (myElement === "木" && targetElement === "金") ||
    (myElement === "火" && targetElement === "水") ||
    (myElement === "土" && targetElement === "木") ||
    (myElement === "金" && targetElement === "火") ||
    (myElement === "水" && targetElement === "土")
  ) {
    relation = "克我";
  } else if (
    (myElement === "木" && targetElement === "火") ||
    (myElement === "火" && targetElement === "土") ||
    (myElement === "土" && targetElement === "金") ||
    (myElement === "金" && targetElement === "水") ||
    (myElement === "水" && targetElement === "木")
  ) {
    relation = "我生";
  } else {
    relation = "生我";
  }

  // 返回六亲关系
  return sixRelativesMap[relation];
}
function getZhangSheng(element, earthBranch) {
  // 验证输入合法性
  const heavenlyStems = [
    "甲",
    "乙",
    "丙",
    "丁",
    "戊",
    "己",
    "庚",
    "辛",
    "壬",
    "癸",
  ];
  const earthlyBranches = [
    "子",
    "丑",
    "寅",
    "卯",
    "辰",
    "巳",
    "午",
    "未",
    "申",
    "酉",
    "戌",
    "亥",
  ];

  if (!heavenlyStems.includes(element) && !earthlyBranches.includes(element)) {
    throw new Error("第一个参数必须是有效的天干或地支");
  }

  if (!earthlyBranches.includes(earthBranch)) {
    throw new Error("第二个参数必须是有效的地支");
  }

  // 定义五行属性映射
  const fiveElementsMap = {
    // 天干五行
    甲: "木",
    乙: "木",
    丙: "火",
    丁: "火",
    戊: "土",
    己: "土",
    庚: "金",
    辛: "金",
    壬: "水",
    癸: "水",
    // 地支五行
    子: "水",
    丑: "土",
    寅: "木",
    卯: "木",
    辰: "土",
    巳: "火",
    午: "火",
    未: "土",
    申: "金",
    酉: "金",
    戌: "土",
    亥: "水",
  };

  // 获取元素的五行属性
  const elementFiveElement = fiveElementsMap[element];

  // 定义五行对应的十二长生顺序（水土同宫）
  const longevityMap = {
    木: [
      "长生",
      "沐浴",
      "冠带",
      "临官",
      "帝旺",
      "衰",
      "病",
      "死",
      "墓",
      "绝",
      "胎",
      "养",
    ],
    火: [
      "长生",
      "沐浴",
      "冠带",
      "临官",
      "帝旺",
      "衰",
      "病",
      "死",
      "墓",
      "绝",
      "胎",
      "养",
    ],
    金: [
      "长生",
      "沐浴",
      "冠带",
      "临官",
      "帝旺",
      "衰",
      "病",
      "死",
      "墓",
      "绝",
      "胎",
      "养",
    ],
    水: [
      "长生",
      "沐浴",
      "冠带",
      "临官",
      "帝旺",
      "衰",
      "病",
      "死",
      "墓",
      "绝",
      "胎",
      "养",
    ],
    土: [
      "长生",
      "沐浴",
      "冠带",
      "临官",
      "帝旺",
      "衰",
      "病",
      "死",
      "墓",
      "绝",
      "胎",
      "养",
    ], // 土随水走，水土同宫
  };

  // 定义五行对应的长生起点地支索引
  const startIndexMap = {
    木: earthlyBranches.indexOf("亥"), // 木长生在亥
    火: earthlyBranches.indexOf("寅"), // 火长生在寅
    金: earthlyBranches.indexOf("巳"), // 金长生在巳
    水: earthlyBranches.indexOf("申"), // 水长生在申
    土: earthlyBranches.indexOf("申"), // 土随水走，水土同宫
  };

  // 获取当前地支的索引
  const targetIndex = earthlyBranches.indexOf(earthBranch);
  // 获取该五行长生起点的索引
  const startIndex = startIndexMap[elementFiveElement];
  // 计算对应的十二长生索引
  let longevityIndex = (targetIndex - startIndex + 12) % 12;

  // 返回对应的十二长生状态
  return longevityMap[elementFiveElement][longevityIndex];
}

function formatDatetoymdhms(date) {
  // 检查输入是否为有效的Date对象
  if (!(date instanceof Date) || isNaN(date.getTime())) {
    throw new Error("请输入有效的Date对象");
  }

  // 获取年、月、日、时、分、秒
  const year = date.getFullYear();
  // 月份从0开始，需要加1，且保持两位数
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");

  // 格式化日期字符串
  const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

  // 获取时间戳（毫秒级）
  return formattedDate;
}

function getSeasonByzmonth(month) {
  if (month === '寅' || month === '卯' || month === '辰') return "春";
  if (month === '巳' || month === '午' || month === '未') return "夏";
  if (month === '申' || month === '酉' || month === '戌') return "秋";
  if (month === '亥' || month === '子' || month === '丑') return "冬";

}
// 示例使用
export {
  formatPickerDate,
  getXunShou,
  zidingyiFormatDatetime,
  getDizhiIdex,
  adjustHours,
  getTianganIndex,
  getLiuQinByGanZhi,
  getZhangSheng,
  getXunKong,
  formatDatetoymdhms,
  getSeasonByzmonth
};
